package com.ycdx.zwzc.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.log4j.Logger;

import com.ycdx.zwzc.pojo.ServiceType;
import com.ycdx.zwzc.utils.DBPool;

public class ServiceTypeImpl {
	static Logger logger = Logger.getLogger(ServiceTypeImpl.class.getName());
	@SuppressWarnings("unchecked")
	public static List<ServiceType> queryAllServiceType(){
		List<ServiceType> typeList = new ArrayList<ServiceType>();
		String sql = "select * from tb_serviceType;";
		
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			typeList = (List<ServiceType>) qr.query(conn, sql, new BeanListHandler(ServiceType.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ServiceTypeImpl.class-queryAllServiceType()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ServiceTypeImpl.class-queryAllServiceType()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return typeList;
	}
	
	/**
	 * 添加入库
	 * @param req
	 * @return
	 */
	public static int addServiceType(ServiceType st){
		String sql = "insert into tb_servicetype(" +
				"name) values(" +
				"?);";	
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		Object[] params = {st.getName()};
		
		int flag = 0;
		try {
			flag = qr.update(conn,sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ServiceTypeImpl.class-addServiceType()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ServiceTypeImpl.class-addServiceType()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
	
	/**
	 * 根据ID获取指定对象
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static ServiceType getServiceTypeById(String id){
		String sql = "select * from tb_servicetype where id=?";
		
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		Object[] params = {id};
		ServiceType st = new ServiceType();
		try {
			st = (ServiceType) qr.query(conn, sql, new BeanHandler(ServiceType.class), params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ServiceTypeImpl.class-getServiceTypeById()-连接关闭失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ServiceTypeImpl.class-getServiceTypeById()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return st;
	}
	
	/**
	 * 更新指定对象
	 * @param st
	 * @return
	 */
	public static int update(ServiceType st){
		String sql = "update tb_servicetype set name=? where id=?";
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		Object[] params = {st.getName(),st.getId()};
		
		int flag = 0;
		try {
			flag = qr.update(conn,sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ServiceTypeImpl.class-update()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ServiceTypeImpl.class-update()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
	
	/**
	 * 删除指定对象
	 * @param st
	 * @return
	 */
	public static int deleteServiceType(String id){
		String sql = "delete from tb_servicetype where id=?";
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		Object[] params = {id};
		
		int flag = 0;
		try {
			flag = qr.update(conn,sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ServiceTypeImpl.class-update()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ServiceTypeImpl.class-update()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
	
}
